package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.RecommendSubject;
import com.itheima.excption.MyException;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.pojo.SmsHomeRecommendProduct;
import com.itheima.pojo.SmsHomeRecommendSubject;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecommendSubjectService {

    @DubboReference
    private RecommendSubjectApi recommendSubjectApi;

    /**
     * 创建首页推荐专题
     *
     * @param recommendSubjectDto
     */
    public void createSubject(List<RecommendSubject> recommendSubjectDto) {
        for (RecommendSubject recommendSubject : recommendSubjectDto) {
            //SmsHomeRecommendSubject
            SmsHomeRecommendSubject smsHomeRecommendSubject = new SmsHomeRecommendSubject();
            if (recommendSubject.getId() != null) {
                smsHomeRecommendSubject.setId(recommendSubject.getId().longValue());
            }
            if (recommendSubject.getSubjectId() != null) {
                smsHomeRecommendSubject.setSubjectId(recommendSubject.getSubjectId().longValue());
            }
            smsHomeRecommendSubject.setSubjectName(recommendSubject.getSubjectName());
            smsHomeRecommendSubject.setSort(recommendSubject.getSort());
            smsHomeRecommendSubject.setRecommendStatus(recommendSubject.getRecommendStatus());
            //根据 专题id查询 是否存在
            //根据商品的id 查询 是否已经被推荐 ,如果已经被推荐 则提示
            SmsHomeRecommendSubject smsHomeRecommendSubjectFind=recommendSubjectApi.findBySid(recommendSubject.getSubjectId());
            if(!ObjectUtils.isEmpty(smsHomeRecommendSubjectFind)){
                //已经被推荐
                throw  new MyException("推荐失败!该专题已经被推荐,请前往列表查看");
            }


            //执行数据库的保存
            recommendSubjectApi.addSubject(smsHomeRecommendSubject);
            //  TODO 是否需要对id重复进行校验? 不用

        }


    }

    /**
     * 根据id的数组进行批量删除
     * @param ids
     */
    public void delete(Integer[] ids) {
        //删除之前,先进行判断选中的id 是否 有启用状态的
        for (Integer id : ids) {
            SmsHomeRecommendSubject byID = recommendSubjectApi.findByID(id);
            if(byID.getRecommendStatus()==1){
                throw new MyException("删除失败,请先修改专题状态,再进行删除");
            }
        }

        recommendSubjectApi.deleteByIds(ids);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @param subjectName
     * @return
     */
    public PageResultVo<RecommendSubject> list(Integer pageNum, Integer pageSize, Integer recommendStatus, String subjectName) {
        IPage<SmsHomeRecommendSubject> page=recommendSubjectApi.list(pageNum,pageSize,recommendStatus,subjectName);
        //返回回来的page对象
        PageResultVo<RecommendSubject> pageResultVo=new PageResultVo<>();
        pageResultVo.setPageNum(pageNum);
        pageResultVo.setPageSize(pageSize);
        Long total = page.getTotal();
        pageResultVo.setTotal(total.intValue());
        Long pages = page.getPages();
        pageResultVo.setTotalPages(pages.intValue());
        List<RecommendSubject> recommendSubjectList=new ArrayList<>();

//        BeanUtils.copyProperties(page.getRecords(),recommendSubjectList);
        List<SmsHomeRecommendSubject> records = page.getRecords();
        recommendSubjectList= records.stream().map((item)->{
            RecommendSubject recommendSubject=new RecommendSubject();
            recommendSubject.setSubjectId(item.getSubjectId().intValue());
            recommendSubject.setId(item.getId().intValue());
            recommendSubject.setSort(item.getSort());
            recommendSubject.setRecommendStatus(item.getRecommendStatus());
            recommendSubject.setSubjectName(item.getSubjectName());
            return recommendSubject;
        }).collect(Collectors.toList());


        pageResultVo.setList(recommendSubjectList);
        return pageResultVo;

    }


    /**
     * 批量修改状态
     * @param recommendStatus
     * @param ids
     */
    public void update(Integer recommendStatus, Integer[] ids) {

        recommendSubjectApi.updateByIds(recommendStatus,ids);

    }


    /**
     * 修改指定的排序
     * @param id
     * @param sort
     */
    public void updateSort(Integer id, Integer sort) {
        SmsHomeRecommendSubject smsHomeRecommendSubject=new SmsHomeRecommendSubject();
        smsHomeRecommendSubject.setSort(sort);
        smsHomeRecommendSubject.setId(id.longValue());
        recommendSubjectApi.update(smsHomeRecommendSubject);

    }
}
